package com.manage.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.common.constant.SystemConstant;
import com.manage.common.util.PageUtils;
import com.manage.sys.entity.SysUser;
import com.manage.sys.mapper.SysUserMapper;
import com.manage.sys.model.SysUserQueryDTO;
import com.manage.sys.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

//import org.springframework.security.core.parameters.P;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author 波哥
 * @since 2022-11-18
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public List<SysUser> queryByUserName(String username) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(username),"username",username);
        wrapper.eq("status", SystemConstant.USER_STATUS_NORMAL);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public PageUtils queryPage(SysUserQueryDTO dto) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(dto.getUsername()),"username",dto.getUsername());
        Page<SysUser> page = this.page(dto.page(), wrapper);
        return new PageUtils(page);
    }

    @Override
    public boolean checkUserName(String username) {

        List<SysUser> list = baseMapper. selectList (new QueryWrapper<SysUser>() .eq("username", username));
        if(list != null && list.size() > 0){
            return true ; //说明账号存在
        }
        return false;
    }

    @Override
    public void saveOrUpdateUser(SysUser sysUser) {
        if(sysUser . getUserId() > 0){
            //更新
        }else{
            //添加
            sysUser.setCreateTime(LocalDateTime.now());
            //设置添加数据的账号
            sysUser. setCreateUserId(getCurrentUserId());
            //密码需要做加密处理
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String password = encoder . encode (sysUser . getPassword());
            sysUser. setPassword(password);
            this. save (sysUser) ;


        }


    }
    public Long getCurrentUserId(){
        //设置添加数据的账号
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken)SecurityContextHolder.getContext().getAuthentication() ;
        String userName = (String) authentication. getPrincipal();
        List<SysUser> list = this . baseMapper . selectList (new QueryWrapper<SysUser>() . eq("username", userName));
        if(list != null && list.size() == 1){
            SysUser sysUser = list .get(0);
            return sysUser . getUserId();
        }
        return null;
    }
}